package org.jeecg.modules.utils.service.impl;

import com.transtella.messageCenter.IMessageManager;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.utils.entity.WmsBaseDocument;
import org.jeecg.modules.utils.service.CommitService;
import org.jeecg.modules.utils.service.IWmsBaseDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 提交单据service实现类
 *
 * @author zhoutong
 */
@Service
public class CommitServiceImpl implements CommitService {

    @Autowired
    private IWmsBaseDocumentService wmsBaseDocumentService;

    @Autowired
    private IMessageManager messageManager;

    @Override
    public Result sendMessage(String id, HttpServletRequest req) {
        WmsBaseDocument document = wmsBaseDocumentService.getById(id);
        if (document == null) {
            return Result.error("未能确定单据类型!");
        }
        int committedRows = wmsBaseDocumentService.updateDocumentToCommitted(document.getId(), document.getTableName());
        if (committedRows != 1) {
            return Result.OK().error500("提交失败,只能提交已保存状态的数据");
        }
        messageManager.sendCommand(req.getHeader(CommonConstant.X_ACCESS_TOKEN),
                "commit." + document.getTableName(), document.getId());
        return Result.OK("提交成功");
    }

}
